package org.wangle.seckill.redis;


import org.wangle.seckill.utils.DoLua;

public class RLock {


    public static final Long EXEC_RESULT = 1L;



    public static void tryLock(String key, String requestId){
        for (;;){
            Long result = DoLua.execute(DoLua.lockScript,new String[]{key},requestId, "3000");
            if(result.intValue() == 2){
                throw new RuntimeException();
            }
            if(EXEC_RESULT.equals(result)){
               // System.out.println("加锁成功");
                return;
            }
            else {
                try {
                    Thread.sleep(200);

                    //System.out.println("该key值的锁已经被占用");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }


    public static void tryLockByFile(String key, String requestId){
        for (;;){
            Long result = DoLua.executeByFile("aaaa" ,key,requestId, "5");
            if(EXEC_RESULT.equals(result)){
                //System.out.println("加锁成功");
                return;
            }
        }
    }

    public static void unLock(String  key, String requestId){
       DoLua.execute(DoLua.unLockScript, new String[]{key}, requestId);
    }






}
